<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>banner管理</title>
    <script src="../commonCss.js"></script>
    <script src="../commonJs.js"></script>
    <link rel="stylesheet" href="../../css/common.css">
</head>

<body class="hold-transition skin-blue sidebar-mini">
<section class="content-header">
    <div class="ctc_title">banner管理</div>
    <div class="down_line"></div>
</section>

<!-- Main content -->
<section class="content" >
    <div class="row">
        <div class="col-xs-12">
            <div class="box-header">
                <form class="form-inline">
                    <label>查询：</label>
                    <div class="form-group">
                        <select class="form-control" id="flag">
                            <option value="">全部</option>
                            <option value="0">下架</option>
                            <option value="1">上架</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <input style="width: 200px;" class="form-control" type="text" id="searchKey"  placeholder="输入banner名称搜索"/>
                    </div>
                    <div class="form-group">
                        <button type="button"  onclick="getData()"  class="btn btn-block btn-primary">搜索</button>
                    </div>
                    <div style="float: right ">
                        <div class="form-group" style="margin-right: 20px;">
                            <button type="button" onclick="addInfo()" class="btn btn-block btn-primary">新增</button>
                        </div>
                    </div>

                </form>
            </div>

            <div class="box-body" style="height:100%;">
                <table id="dataList" class="table table-bordered table-hover" >
                    <thead>
                    <tr>
                        <th>banner</th>
                        <th>banner名称</th>
                        <th>跳转App内部</th>
                        <th>跳转内部URL</th>
                        <th>跳转外部URL</th>
                        <th>排序</th>
                        <th>状态</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody >
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</section>

<!-- 【编辑】弹出窗内容 -->
<div class="modal" id="editPanel" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog" style="width: 50%;height: 40%;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">
                    <span aria-hidden="true">×</span>
                    <span class="sr-only">Close</span>
                </button>
                <h4 class="modal-title" align="center" id="mopTitle"></h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" onsubmit="return false" id="form">
                    <fieldset  style="margin-top:2%;">
                        <input type="hidden" id="id" name="id">
                        <div class="form-group" >
                            <label class="col-md-2 control-label red">banner名称：</label>
                            <div class="col-md-10">
                                <input class="form-control"  type="text" name="name" id="bannerName" >
                            </div>
                        </div>
                        <div class="form-group" >
                            <label class="col-md-2 control-label" >跳转至App内部：</label>
                            <div class="col-md-10">
                                <select class="form-control" name="sysConfigId" id="sysConfigId">
                                    <option value="0">--不跳转到App内部--</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group" >
                            <label class="col-md-2 control-label" >跳转外部URL：</label>
                            <div class="col-md-10">
                                <input class="form-control" placeholder="" type="text" name="url" id="url">
                            </div>
                        </div>
                        <div class="form-group" >
                            <label class="col-md-2 control-label"  style="color: red">*banner图片：</label>
                            <div class="col-md-10">
                                <img id="pictue" style="width: 350px;height: 88px;"><br/>
                                <input class="form-control-file" type="file" id="uploadFile" name="file" accept="image/png,image/gif,image/jpeg,image/jpg" onchange="uploadPic()"/>
                                <input type="hidden" name="img" id="img">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-md-2 control-label">排序：</label>
                            <div class="col-md-10">
                                <input  class="form-control" type="number" name="sort" id="sort" style="width: 20%;" >
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-md-2 control-label">状态：</label>
                            <div class="col-md-10">
                               <select class="form-control" id="status" name ="status" style="width: 30%;">
                                    <option value="1">上架</option>
                                    <option value="0">下架</option>
                               </select>
                            </div>
                        </div>

                    </fieldset>
                </form>
            </div>
            <div class="modal-footer" style="text-align: center">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" onclick="confirmEditConfig();">确定</button>
            </div>
        </div>
    </div>
</div>

<!-- 【图片点击放大】弹出窗内容 -->
<div class="modal" id="picMonthDialog" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true" >
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" align="center">banner</h4>
            </div>
            <img id="maxPic" width="100%">
        </div>
    </div>
</div>

<!-- page script -->
<script>
    $(document).ready(function() {
        //加载数据
        getData();
        initApp();
    });

    // 加载数据
    function getData(){
        var table = $('#dataList').DataTable({
            "destroy": true,   //可以重新初始化，用于搜索
            "lengthChange": false,
            "ordering": false,   //排序
            "info": true,		   //
            "autoWidth": false,   //宽度自动
            "paging": true,      //分页
            "searching": false,  //全局搜索关闭
            "processing": true,  //开启显示‘正在加载.../processing’
            "serverSide":true,    //true代表后台处理分页，false代表前台处理分页
            "bPaginate": false, //开关，是否显示分页器
            "bLengthChange": false, //开关，是否显示每页显示多少条数据的下拉框
            "ajax" :  {
                url:"/banner/getBannerList",
                type:"POST",
                data:{
                    key : $("#searchKey").val(),
                    flag : $("#flag").val()
                },
                dataSrc: function (myJson) {
                    return myJson.data.data;
                }
            },
            //参数显示
            "columns": [
                {"data": "img", 'sClass': "text-center",'width': '8%',
                    "render": function ( data, type, full, meta ) {
                        return "<img style='width: 330px;height: 88px; ' src='"+data+"'onclick=maxPic('"+data+"')>";
                    }
                },
                {"data": "name", 'sClass': "text-center",'width': '8%'},
                {"data": "appName", 'sClass': "text-center",'width': '8%'},
                {"data": "appUrl", 'sClass': "text-center",'width': '8%'},
                {"data": "url", 'sClass': "text-center",'width': '10%'},
                {"data": "sort", 'sClass': "text-center",'width': '3%'},
                {"data": "status", 'sClass': "text-center",'width': '5%',
                    "render":function(data,type,full,meta){
                        var str="";
                        if(data == 0){
                            str="<span class='label label-danger' style='padding-left:10px;padding-right:10px;padding-top: inherit;'>下架</span>";
                            return str;
                        }else{
                            str="<span class='label label-success' style='padding-left:10px;padding-right:10px;padding-top: inherit;' >上架</span> ";
                            return str;
                        }
                    }
                },
                {"data": "id" , 'sClass': "text-center" ,'width':'5%',
                    "render": function (data, type, full, meta ) {
                        var str = "<a href = '#' title='修改' onclick=editBanner(" + data +");><i class='\glyphicon glyphicon-pencil'></i></a>&nbsp;&nbsp;&nbsp;";
                        str += "<a href = '#' title='删除' onclick=deleteBanner(" + data +");><i class='\glyphicon glyphicon-trash'></i></a>&nbsp;&nbsp;&nbsp;";
                        return str;
                    }
                }
            ],

            //是否开启主题
            "bJQueryUI": true,
            "oLanguage": {    // 语言设置
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "sZeroRecords": "没有检索到数据",
                "sSearch": "检索:",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                }
            }
        });
    }

    //编辑
    function editBanner(id) {
        $("#pictue").attr("src","");
        $.ajax({
            type:"POST",
            url:"/banner/getBannerById",
            async : true,//默认就是true
            dataType:"json",
            data:{id : id},
            success : function(res) {
                var data = res.data.banner;
                $("#id").val(data.id);
                $("#bannerName").val(data.name);
                $("#img").val(data.img);
                $("#pictue").attr("src",data.img);
                $("#url").val(data.url);
                $("#sort").val(data.sort);
                $("#status").val(data.status);
                $("#sysConfigId").val(data.sysConfigId);
            }
        });
        $("#mopTitle").text("编辑banner");
        $("#editPanel").modal("show");//弹窗打开
    }

    //打开新增窗口
    function addInfo() {

        $("#id").val("");
        $("#bannerName").val("");
        $("#img").val("");
        $("#url").val("");
        $("#pictue").attr("src","");
        $("#sort").val("1");
        $("#status").val("1");

        $("#mopTitle").text("新增banner");
        $("#editPanel").modal("show");//弹窗打开
    }

    //保存
    function confirmEditConfig(){
        var img = $("#img").val();

        if(img == ""){
            layer.msg("banner图片不能为空！");
            return ;
        }
        var formdata = $("#form").serializeObject();

        $.ajax({
            type : 'post',
            url : "/banner/updateBanner",
            contentType: "application/json; charset=utf-8",
            data : JSON.stringify(formdata),
            success : function(data) {
                layer.msg(data.message);
                $("#editPanel").modal("hide");
                getData();
            }
        });
    }
    //上传图片
    function uploadPic() {
        var formData=new FormData();
        formData.append("pic",document.getElementById("uploadFile").files[0]);
        $.ajax({
            type : 'post',
            url : "/banner/uploadFile",
            data:formData,
            contentType: false,
            processData: false,
            success : function(data) {
                let url = data.data.url;
                $("#pictue").attr("src",url);
                $("#img").val(url)
            }
        });
    }

    //删除
    function deleteBanner(id){
        if(confirm("确定要删除吗？")){
            $.ajax({
                type:"POST",
                url:"/banner/deleteBanner",
                async : true,//默认就是true
                dataType:"json",
                data:{id : id},
                success : function(data) {
                    if(!data.error){
                        layer.msg(data.message);
                        getData();
                    }else{
                        layer.msg(data.message);
                    }

                }
            });
        }
    }

    //初始化下拉选择app跳转内部模块
    function initApp() {
        $.ajax({
            type:"POST",
            url:"/sysconfig/getAllSysConfig",
            async : true,//默认就是true
            dataType:"json",
            data:{},
            success : function(res) {
                var sysConfigsList = res.data.sysConfigsList;
                for (var i = 0; i <sysConfigsList.length ; i++) {
                    $("#sysConfigId").append("<option value='"+sysConfigsList[i].id +"'>"+sysConfigsList[i].remark+"</option>");
                }
            }
        });
    }


</script>
</body>
</html>
